﻿#include<iostream>
using namespace std;

// 计算整数x中1的位数
int count_bits(unsigned x) {
	int bits = 0;
	while (x & 1U) {
		bits++;
		x >>= 1;
	}
	return bits;
}

// 计算int型或unsigned型的位数
int int_bits() {
	return count_bits(~0U);
}

// 显示unsigned型的所有位
void print_bits(unsigned x) {
	for (int i = int_bits() - 1; i >= 0; i--) {
		cout << ((x >> i) & 1U ? '1' : '0');
	}
}

int main() {
	unsigned a, b;
	cout << "请输入两个非负整数\n";
	cout << "a："; cin >> a;
	cout << "b："; cin >> b;

	cout << "a     = "; print_bits(a); cout << '\n';
	cout << "b     = "; print_bits(b); cout << '\n';
	cout << "a & b = "; print_bits(a & b); cout << '\n';
	cout << "a | b = "; print_bits(a | b); cout << '\n';
	cout << "a ^ b = "; print_bits(a ^ b); cout << '\n';
	cout << "~a    = "; print_bits(~a); cout << '\n';
	cout << "~b    = "; print_bits(~b); cout << '\n';

	cin.get();
	cin.get();
}